package com.lvy.hczn.front.business.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.business.dto.Param;
import com.lvy.hczn.front.business.dto.SearchCondition;
import com.lvy.hczn.front.system.domain.business.*;
import com.lvy.hczn.front.business.service.*;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.core.domain.BaseModel;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.service.SpuService;
import com.lvy.hczn.front.system.service.WarehouseService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/wcs/sale/order/")
@RestController
@Slf4j
public class SaleOrderController {

    @Autowired
    private SaleOrderService saleOrderService;

    @Autowired
    private SaleOrderDetailService saleOrderDetailService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private SortTaskDetailService sortTaskDetailService;

    @Autowired
    private SortTaskService sortTaskService;

    @Autowired
    private SpuService spuService;

    @ApiOperation(value = "初始化销售单跟分拣详情", notes = "初始化销售单跟分拣详情")
    @PostMapping("test")
    public ResultUtil test(@RequestBody Map<String, Object> map) {
        try {
            for (int j = 0; j < 1; j++) {
                SaleOrder saleOrder = new SaleOrder();
                List<SaleOrderDetail> list = new ArrayList<>();
                saleOrder.setSaleOrderCode(IdUtil.nanoId());
                saleOrder.setMemberCode(IdUtil.getSnowflake().nextIdStr());
                saleOrder.setName("郭嫣允");
                saleOrder.setPhone("18678328921");
                saleOrder.setAreaName("湖南省长沙市");
                saleOrder.setAddress("湖南省长沙市星沙区环通物流园");
                Warehouse warehouse = warehouseService.selectLocalWarehouseFront();
                if (warehouse != null) {
                    saleOrder.setWarehouseCode(warehouse.getWarehouseCode());
                }
                saleOrder.setDeliveryTime(LocalDateTime.now());
                saleOrder.setPaymentCode(IdUtil.getSnowflake().nextIdStr());
                saleOrder.setDeliveryArea("星沙区");
                List<String> ids = new ArrayList<>();
                List<Spu> spuList = new ArrayList<>();
                if (map != null && map.containsKey("list")) {
                    List<String> idList = (List<String>) map.get("list");
                    spuList = spuService.list(Wrappers.<Spu>lambdaQuery().in(BaseModel::getId, idList));
                } else {
                    spuList = spuService.list();
                }

                ids = spuList.stream().map(BaseModel::getId).collect(Collectors.toList());
                for (int i = 0; i < ids.size(); i++) {
                    SaleOrderDetail saleOrderDetail = new SaleOrderDetail();
                    saleOrderDetail.setSaleOrderCode(saleOrder.getSaleOrderCode());
                    saleOrderDetail.setGoodsCode(ids.get(i));
                    saleOrderDetail.setPrice(RandomUtil.randomBigDecimal(BigDecimal.valueOf(1), BigDecimal.valueOf(1000)));
                    saleOrderDetail.setPrice(RandomUtil.randomBigDecimal(BigDecimal.valueOf(1), BigDecimal.valueOf(1000)));
                    saleOrderDetail.setQuantity(2);
                    saleOrderDetail.setUnit("瓶");
                    String id = String.valueOf(ids.get(i));
                    if (id.equals("44")) {
                        //今麦郎0.57
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.57));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.57));
                    } else if (id.equals("45")) {
                        //百事可乐
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.32));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.32));
                    } else if (id.equals("46")) {
                        //七喜(西柚味)
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.59));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.59));
                    } else if (id.equals("47")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.59));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(2.95));
                    } else if (id.equals("48")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(1.07));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(1.07));
                    } else if (id.equals("50")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.53));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.53));
                    } else if (id.equals("51")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.48));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.48));
                    } else if (id.equals("52")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.65));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.65));
                    } else if (id.equals("53")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.65));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.65));
                    } else if (id.equals("54")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.48));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.48));
                    } else if (id.equals("55")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.48));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.48));
                    } else if (id.equals("57")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.53));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.53));
                    } else if (id.equals("58")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.54));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.54));
                    } else if (id.equals("59")) {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.52));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.52));
                    } else {
                        saleOrderDetail.setWeight(BigDecimal.valueOf(0.57));
                        saleOrderDetail.setActWeight(BigDecimal.valueOf(0.57));
                    }

                    saleOrderDetail.setStatus("0");
                    saleOrderDetail.setTakeDeliveryOf(1);
                    list.add(saleOrderDetail);
                }
                SortTask sortTask = new SortTask();
                sortTask.setWarehouseCode(saleOrder.getWarehouseCode());
                sortTask.setClientId("10");
                sortTask.setTaskType("3");
                sortTask.setStatus("1");
                sortTask.setSourceId("0");
                sortTaskService.save(sortTask);
                saleOrderService.save(saleOrder);
                saleOrderDetailService.saveBatch(list);
                sortTaskDetailService.initTaskDetailBySaleOrder(saleOrder, list, sortTask, false);
            }
            return ResultUtil.ok();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "生成分拣任务", notes = "生成分拣任务")
    @PostMapping("init")
    public ResultUtil init() {
        try {
            sortTaskDetailService.generateSortTaskByDeliveryArea(null, null);
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("init:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    /**
     * 退货拒收列表
     *
     * @return
     */
    @PostMapping("return/list")
    @ApiOperation(value = "退货拒收列表", notes = "退货拒收列表")
    public ResultUtil returnList(@RequestBody Param param) {
        return ResultUtil.ok(saleOrderService.returnList(param));
    }

    /**
     * 首页显示统计数量
     *
     * @return
     */
    @PostMapping("return/count")
    @ApiOperation(value = "首页显示统计数量", notes = "首页显示统计数量")
    public ResultUtil returnCount(@RequestBody SaleOrderDetail saleOrderDetail) {
        return ResultUtil.ok(saleOrderService.returnCount(saleOrderDetail));
    }

    /**
     * 退货拒收入回调
     *
     * @return
     */
    @PostMapping("return/back")
    @ApiOperation(value = "退货拒收入回调", notes = "退货拒收入库离开缓存位回调业务处理")
    public ResultUtil returnCallBack(@RequestBody SearchCondition param) {
        saleOrderService.returnCallBack(param);
        return ResultUtil.ok();
    }
}
